<?php
namespace cms\modules\frontend\controllers;

use Yii;
use yii\helpers\Html;
use yii\db\Expression;
use app\models\Tplinfo;
use app\modules\frontend\models\ContentCategory;
use app\modules\frontend\models\ContentCategorySearch;
use app\modules\frontend\models\Nav;
use app\modules\frontend\models\NavSearch;
use app\modules\frontend\models\Content;
use app\modules\frontend\models\ContentSearch;
use app\modules\frontend\models\ContentAttrRelationships;
use app\modules\frontend\models\TagsRelationships;
use common\component\tool\Tool;
use common\component\exception\ErrMap;
use yii\smarty\ViewRenderer;


class SmartyController extends AuthController
{
    /*PC*/
    public function actionIndex()
    {
        $tplName = '';
        // P($this->get);
        /*根据栏目别名，获取模板*/
        if(isset($this->get['category']) and !empty($this->get['category'])){
            /*匹配栏目*/
            $tpl = ContentCategory::find()
                        ->select(['flag', 'page_tpl', 'list_tpl', 'detail_tpl'])
                        ->where('app_id = :app_id and alias = :alias', [':app_id' => $this->app_id, ':alias' => $this->get['category']])
                        ->asArray()->one();
            // P($tpl);
            if(!is_null($tpl)){
                if($tpl['flag'] == 1 and !empty($tpl['page_tpl'])){//单页
                    $tplName = $tpl['page_tpl'];
                    // P($tplName);
                }else if($tpl['flag'] == 2 and !empty($tpl['list_tpl'])){//列表
                    if(isset($this->get['id'])){//如果是传入了id参数，说明是需要详情模板
                        $tplName = $tpl['detail_tpl'];
                    }else{
                        $tplName = $tpl['list_tpl'];
                    }
                }
            }else{//如果没有匹配到栏目，以传入的category的名称为模板名称
                $tplName = $this->get['category'] . '.html';
            }

        }else{
            $tplName = 'index.html';
        }
        // P($tplName);
        if(empty($tplName)){
            /*注释原因：直接跳转到404页面*/
            // echo '未设置模板';die;
            header("HTTP/1.1 303 See Other");
            header('Location: 404.html');
        }else{
          /*取出模板路径*/
          $tplinfo = Tplinfo::find()->select(['tplfolder', 'folder'])
                      ->where('id = :id', [':id' => $this->template])->one();
          // P($tplinfo);
          if(!empty($tplinfo->tplfolder)){
            $this->template = $tplinfo->tplfolder;
          }
          $folder = '';
          if(!empty($tplinfo->folder)){
            $folder = '/' . $tplinfo->folder;
          }
          $tplUrl = Yii::$app->params['templates'] . '/' . $this->appid . '/' . $this->template . $folder;
        }
        /*查看模板是否存在*/
        // P($tplUrl . '/'. $tplName);
        if(!file_exists($tplUrl . '/'. $tplName)){
          /*注释原因：直接跳转到404页面*/
            // echo '模板文件不存在';die;
          header("HTTP/1.1 303 See Other");
          header('Location: 404.html');
        }
        // P($tplUrl);
        // P($this->memberInfo);
        if(isset($this->memberInfo)){
            $member_info = array(
                'loginname' => $this->memberInfo->loginname,
                'phone' => $this->memberInfo->phone,
                'email' => $this->memberInfo->email,
                'nickname' => $this->memberInfo->nickname,
                'sex' => $this->memberInfo->sex,
                'signature' => $this->memberInfo->signature,
                'headimgurl' => $this->memberInfo->headimgurl,
                'add_time' => $this->memberInfo->add_time
            );
        }else{
            $member_info = array(
                'loginname' => '',
                'phone' => '',
                'email' => '',
                'nickname' => '',
                'sex' => '',
                'signature' => '',
                'headimgurl' => '',
                'add_time' => ''
            );
        }
        // P($member_info);
        // P($_SERVER);
        // P($this->get);
        // P($tplUrl . '/'. $tplName);
        if(!isset($this->get['category'])){
            $this->get['category'] = '';
        }

        return $this->renderFile($tplUrl . '/'. $tplName, [
            'siteurl' => $this->domain . '/',
            'static_path' => $this->domain . '/' . $this->appid . '/' . $this->template . $folder,
            'member_info' => $member_info,
            'get' => $this->get,
            // 'post' => $this->post,
            // 'server' => $_SERVER,
        ]);
    }

    /*PC*/
    /*注释原因：此函数是开发东海期货的函数，做个备份，可以参考*/
   //  public function actionIndex()
   //  {
   //      // $smarty = new Smarty;
   //      // $smarty = Yii::$app->smarty->smarty;
   //      // $smarty->registerPlugin('function', 'jiang', '\common\component\tool\Tool::getMsectime');
   //      // $smarty->registerPlugin('function', 'jiang', 'test');
   //      // $source = Yii::$app->redis;
   //      // P($smarty);
   //      // P($source);
   //      // P($_SERVER);
   //      $tplName = '';
   //  	// P($this->get);
   //      /*根据栏目别名，获取模板*/
   //      if(isset($this->get['category']) and !empty($this->get['category'])){
			// /*匹配栏目*/
   //      	$tpl = ContentCategory::find()
   //                      ->select(['flag', 'page_tpl', 'list_tpl', 'detail_tpl'])
   //  					->where('app_id = :app_id and alias = :alias', [':app_id' => $this->app_id, ':alias' => $this->get['category']])
   //  					->asArray()->one();
   //      	// P($tpl);
   //      	if(!is_null($tpl)){
   //      		if($tpl['flag'] == 1 and !empty($tpl['page_tpl'])){//单页
   //                  $tplName = $tpl['page_tpl'];
   //                  // P($tplName);
   //              }else if($tpl['flag'] == 2 and !empty($tpl['list_tpl'])){//列表
   //                  if(isset($this->get['id'])){//如果是传入了id参数，说明是需要详情模板
   //                      $tplName = $tpl['detail_tpl'];
   //                  }else{
   //                      $tplName = $tpl['list_tpl'];
   //                  }
   //              }
   //      	}else{//如果没有匹配到栏目，以传入的category的名称为模板名称
   //              $tplName = $this->get['category'] . '-2.html';
   //          }

   //      }else{
   //          $tplName = 'index-2.html';
   //      }
   //      // P($tplName);
   //      if(empty($tplName)){
   //          echo '未设置模板';die;
   //      }else{
   //          $tplUrl = Yii::$app->params['templates'] . $this->template;
   //      }
   //      /*查看模板是否存在*/
   //      if(!file_exists($tplUrl . '/pages/'. $tplName)){
   //          echo '模板文件不存在';die;
   //      }
   //      // P($tplUrl);
   //      // P($this->memberInfo);
   //      if(isset($this->memberInfo)){
   //          $member_info = array(
   //              'loginname' => $this->memberInfo->loginname,
   //              'phone' => $this->memberInfo->phone,
   //              'email' => $this->memberInfo->email,
   //              'nickname' => $this->memberInfo->nickname,
   //              'sex' => $this->memberInfo->sex,
   //              'signature' => $this->memberInfo->signature,
   //              'headimgurl' => $this->memberInfo->headimgurl,
   //              'add_time' => $this->memberInfo->add_time
   //          );
   //      }else{
   //          $member_info = array(
   //              'loginname' => '',
   //              'phone' => '',
   //              'email' => '',
   //              'nickname' => '',
   //              'sex' => '',
   //              'signature' => '',
   //              'headimgurl' => '',
   //              'add_time' => ''
   //          );
   //      }
   //      // P($member_info);
   //      // P($_SERVER);
   //      // P($this->get);
   //      // P($tplUrl . '/pages/'. $tplName);
   //      if(!isset($this->get['category'])){
   //          $this->get['category'] = '';
   //      }
   //      return $this->renderFile($tplUrl . '/pages/'. $tplName, [
   //          'siteurl' => $this->domain . '/',
   //          'static_path' => $this->domain . '/' . $tplUrl,
   //          'member_info' => $member_info,
   //          'get' => $this->get,
   //          'post' => $this->post,
   //          // 'server' => $_SERVER,
   //      ]);
   //  }


}
